<< define "role/create.html" >>
<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>编辑角色</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8"/>
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <link rel="stylesheet" href="/static/css/mbgadmin.css">
    <script src="/static/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <script type="text/javascript" src="/static/js/mbgadmin.js"></script>
</head>
<body>
<form class="layui-form" lay-filter="roles_edit_form">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称<span class="x-red">*</span></label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" placeholder="请输入角色名称" autocomplete="off"
                   class="layui-input" value="">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色描述</label>
        <div class="layui-input-block">
            <input type="text" name="describe" placeholder="请输入角色描述" autocomplete="off"
                   class="layui-input" value="">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态<span class="x-red">*</span></label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="1" title="启用" checked>
            <input type="radio" name="status" value="0" title="禁用" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色菜单<span class="x-red">*</span></label>
        <div class="layui-input-block">
            <div id="menus"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="addRole" id="submit">立即提交</button>
        </div>
    </div>
</form>
<script>

        layui.use(['tree', 'jquery', 'form','layer'], function () {
            var tree = layui.tree;
            var $ = layui.jquery;
            var form = layui.form;
            var layer = layui.layer;

            var tree_data =[]
            $.get("/menus/menusTreeList",function (res) {
                //渲染
                var  data={
                    elem: '#menus'  //绑定元素
                    , id: 'createmenus'
                    , showCheckbox: true
                    ,data:res.data
                }
                tree_data = res.data
                tree.render(data);
            })

            form.on('submit(addRole)', function (data) {
                $("#submit").addClass("layui-btn-disabled") //防止连续点击添加键。
                    // 调用上面的函数来获取选中节点的ID
                    var checkedIds = [];
                    getSelectedNodeIds(tree.getChecked('createmenus'), checkedIds);
                    let loadIndex = layer.load(2);
                    $.post("/roles/create", JSON.stringify({ menuIDs: checkedIds,name:data.field.name,describe:data.field.describe,status:data.field.status}), function(data) {
                        if (data.code===0){
                            layer.close(loadIndex)
                            parent.layer.msg(data.msg);
                            ReloadLists()
                        }else {
                            layer.close(loadIndex)
                            layer.msg(data.msg)
                            $("#submit").removeClass("layui-btn-disabled")
                        }
                    })

                return false;
            })
        });
        function getSelectedNodeIds(treeData, checkedIds) {
            layui.each(treeData, function (index, node) {
                if (node.children && node.children.length > 0) {
                    // 如果节点有子节点，递归处理子节点
                    getSelectedNodeIds(node.children, checkedIds);
                } else if (node.checked) {
                    // 如果节点被选中且没有子节点，添加其ID到checkedIds数组中
                    checkedIds.push(node.id);
                }
            });
        }
</script>
</body>
</html>
<< end >>